package com.it.controller.admin;


import com.it.domain.BlogTypeEntity;
import com.it.service.BlogTypeService;
import com.it.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : code1997
 * @date : 2021/9/9 22:46
 */
@Controller
@RequestMapping("/admin")
public class BlogTypeController {

    private static final String TYPES_URL = "admin/types";
    private static final String TYPES_INPUT_URL = "admin/types-input";

    @Autowired
    private BlogTypeService typeService;


    /**
     * 默认size=10
     */
    @GetMapping("/types")
    public String toTypesPage(@RequestParam(defaultValue = "1") String page, Model model) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        PageUtils curPage = typeService.listType(params);
        model.addAttribute("page", curPage);
        return TYPES_URL;
    }

    /**
     * 跳转到Types-input页面
     */
    @GetMapping("/types/input")
    public String toInputPage(Model model) {
        model.addAttribute("type", new BlogTypeEntity());
        return TYPES_INPUT_URL;
    }

    /**
     * 保存类型的方法：进行非空判断.
     *
     * @param blogTypeEntity ：传入的type类型
     * @param result         ：封装校验结果
     * @param attributes     ：传入响应的数据
     * @return ：到的页面。
     */
    @PostMapping("/types")
    public String saveType(@Valid BlogTypeEntity blogTypeEntity, BindingResult result, RedirectAttributes attributes) {
        //该校验需要hasErrors之前
        if (typeService.getTypeByName(blogTypeEntity.getName()) != null) {
            result.rejectValue("name", "nameError", "该分类名称已经存在，不能重复添加！！！");
        }
        if (result.hasErrors()) {
            return TYPES_INPUT_URL;
        }
        BlogTypeEntity blogTypeEntity1 = typeService.saveType(blogTypeEntity);
        if (blogTypeEntity1 == null) {
            attributes.addFlashAttribute("message", "添加失败,分类的名称为【" + blogTypeEntity.getName() + "】！！！");
        } else {
            attributes.addFlashAttribute("message", "添加分类【" + blogTypeEntity.getName() + "】成功！！！");
        }
        return "redirect:/" + TYPES_URL;
    }

    /**
     * 用于数据回显
     */
    @GetMapping("/types/{id}/input")
    public String editInput(@PathVariable("id") Long id, Model model) {
        model.addAttribute("type", typeService.getType(id));
        return TYPES_INPUT_URL;
    }

    @PostMapping("/types/{id}")
    public String editType(@Valid BlogTypeEntity blogTypeEntity, BindingResult result, @PathVariable("id") Long id, RedirectAttributes attributes) {
        if (typeService.getTypeByName(blogTypeEntity.getName()) != null) {
            result.rejectValue("name", "nameError", "该分类名称已经存在，不能重复添加！！！");
        }
        if (result.hasErrors()) {
            return TYPES_INPUT_URL;
        }
        BlogTypeEntity blogTypeEntity1 = typeService.updateType(id, blogTypeEntity);
        if (blogTypeEntity1 == null) {
            attributes.addFlashAttribute("message", "修改失败,分类的名称为【" + blogTypeEntity.getName() + "】！！！");
        } else {
            attributes.addFlashAttribute("message", "修改分类【" + blogTypeEntity.getName() + "】成功！！！");
        }
        return "redirect:/" + TYPES_URL;
    }

    @GetMapping("/types/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {
        typeService.deleteType(id);
        attributes.addFlashAttribute("message", "删除分类成功！！！");
        return "redirect:/" + TYPES_URL;
    }

}
